$(function(){
	var url={
		list:'{:url("adminapi/CarBuy/buyList")}',//列表
		delete:'{:url("adminapi/CarBuy/delete")}',//删除
		// http://127.0.0.1/adminapi/car_buy/delete.html?car_audit_id=1
		check:'{:url("adminapi/CarBuy/checkBuy")}',//审核
        // http://127.0.0.1/adminapi/car_buy/checkBuy.html?car_audit_id=1&state=2
        add:'{:url("adminapi/CarBuy/checkBuy")}'//新增
		//http://127.0.0.1/adminapi/car_buy/add.html?company_id=1&user_name=1&car_brand_id=1&car_series_id=2&car_model_id=3&people=4&money=5&buyto=6&whybuy=7
		
	}
	var page = 1;
	var tddata = [{
		key: 'apply_buycar_no',
		type: 1,
		url: ''
	}, {
		key: 'brand',
		type: 1,
		url: '{:url("detail")}',
		id: 137
	}, {
		key: 'car_type_name',
		type: 1,
		url: ''
	}, {
		key: 'company_name',
		type: 1,
		url: ''
	}, {
		key: 'apply_time',
		type: 1,
		url: ''
	}, {
		key: 'state2',
		type: 1,
		url: ''
	}];
	var addoperation = `<td>
						<div class="tpl-table-black-operation operate">
							<a href="javascript:;" class="tpl-table-black-operation-del scrap_close">
								<i class="am-icon-pencil"></i> 删除
							</a>
						</div>
					</td>`
	var data={};
	data.page=page;
	data.pagenum=10;
	var where_json={};
	{if $Think.get.car_audit_id}
		where_json.car_audit_id=['like',"%{$Think.get.car_audit_id}%"];
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like',"%{$Think.get.company_name}%"];
	{/if}
	{if $Think.get.number}
		where_json.number=['like',"%{$Think.get.number}%"];
	{/if}
	{if $Think.get.state}
		where_json.state=['in',"{$Think.get.state}"];
	{/if}
	
	data.where_json=JSON.stringify(where_json)
	ajaxbuycar(data)
	$('.morefile').on('click',function(){
		morefile1=layer.open({
		  type: 1,
		  title:false,
		  skin: 'layui-layer-rim', //加上边框
		  area: ['200px', '120px'], //宽高
		  content: `<div class="" style="height:100%;width:100%;text-align:center;padding:10px 0;">
		  	<button class="morefile_layer am-btn am-btn-sm am-btn-primary am-radius" style="margin-top:10px">请点击上传文件</button>
		  </div>`,
		  success:function(){
		  	$('.morefile_layer').on('click',function(){
		  		$('.carmorefile').click()
		  	})
		  	$('.carmorefile').on('change',function(){
				var filemain=`
					<div class="morefile_layer">${$('.carmorefile')[0].files[0].name}</div>
					<div><button class="am-btn am-btn-xs file_ok ">确定</button><button  class="am-btn am-btn-xs file_no" style='margin-left:2px'>取消</button></div>
				`
				$('.morefile_layer').parent().html(filemain)
				$('.morefile_layer').on('click',function(){
			  		$('.carmorefile').click()
			  	})
				$('.file_ok').on('click',function(){
			  		Msg('上传成功')
			  		layer.close(morefile1)
			  	})
				$('.file_no').on('click',function(){
			  		layer.close(morefile1)
			  	})
			})
		  }
		});
		
	})
	
	
	function ajaxbuycar(data){
		Load()
		$.ajax({
			type:"post",
			url:url.list,
			data:data,
			success:function(res){
				console.log(res)
				Load(1)
				

				if(res.code==1){
					for(var stateidx=0;stateidx<res.data.length;stateidx++){
						if(res.data[stateidx].state==1){
							res.data[stateidx].state2='待审核'
						}else if(res.data[stateidx].state==2){
							res.data[stateidx].state2='已审核'
						}else if(res.data[stateidx].state==3){
							res.data[stateidx].state2='审核不通过'
						}else if(res.data[stateidx].state==4){
							res.data[stateidx].state2='已登记'
						}else if(res.data[stateidx].state==5){
							res.data[stateidx].state2='已审批'
						}
					}
					$('tbody').html(tbodymainf2(res.data, 1, tddata, addoperation,  'car_audit_id'))
					$('.pagination').html(pagechang1(res.count, 10, page))
					$('tbody tr').each(function(){
						var operate='';
						for(var idx=0;idx<res.data.length;idx++){
							
							if(res.data[idx].car_audit_id==$(this).data('id')){
								if(res.data[idx].state==1){
									operate=`<a class='buy_check'>
											<i class="am-icon-pencil "></i> 审核
										</a>`
									
								}else if(res.data[idx].state==2){
									operate=`
										<a class='detail' data-type='2'>
											<i class="am-icon-pencil"></i> 详情
										</a>
										<a href='{:url("add")}?car_audit_id=`+res.data[idx].car_audit_id+`'>
											<i class="am-icon-pencil "></i> 登记
										</a>
										`
								}else if(res.data[idx].state==4){
									operate=`
										<a class='detail' data-type='3'>
											<i class="am-icon-pencil "></i> 详情
										</a>
										<a href='{:url("detail")}?car_audit_id=`+res.data[idx].car_audit_id+`' class='car_check'>
											<i class="am-icon-pencil "></i> 车辆审批
										</a>
										`
								}else {
									operate=`
										<a class='detail' data-type='4'>
											<i class="am-icon-pencil "></i> 详情
										</a>`
								}
								
							}
						}
						$(this).find('.operate').append(operate)
					})
					
					
					$('.laquo').on('click',function(){
						page-=1;
						
						data.page=page
						ajaxbuycar(data)
					})
					$('.pageli').on('click',function(){
						page=$(this).html()
						data.page=page
						ajaxbuycar(data)
					})
					//下一页
					$('.raquo').on('click',function(){
						page+=1;
						data.page=page
						ajaxbuycar(data)
					});
					$('.buy_check').on('click',function(){
						for(var i=0;i<res.data.length;i++){
							if($(this).closest('tr').data('id')==res.data[i].car_audit_id){
								
								propcheck(res.data[i])
							}
						}
					})
					$('.register').on('click',function(){
						window.location.href='{:url("add")}?audit=1'
					})
					//详情
					$('.detail').on('click',function(){
						for(var i=0;i<res.data.length;i++){
							if($(this).closest('tr').data('id')==res.data[i].car_audit_id){
								console.log(res.data[i])
								auditdetail(res.data[i])
							}
						}
						
					})
					$('.scrap_close').on('click',function(){
						scrapclose($(this))
					})
				}else{
					Err('请求失败')
				}
			}
		});
	}
	//删除
	function scrapclose(obj){
		
		var auto = layer.open({
   		type: 1,
   		title:false,
   		skin: 'layui-layer-rim', //加上边框
   		area: ['200px', '100px'], //宽高
   		content: `
   			<div class='am-u-sm-12' style='padding:10px'>
   				<div class='am-u-sm-12' style='text-align:center;margin-bottom:10px'>是否删除该申购单</div>
   				<div class='am-u-sm-12' style='text-align:center'>
   					<a class='am-btn am-btn-sm am-btn-primary am-radius close_ok'>是</a>
   					<a class='am-btn am-btn-sm am-btn-primary am-radius close_no'>否</a>
   				</div>
   			</div>
   		`,
   		success:function(){
   			
   			$('.close_ok').on('click',function(){
   				var id=obj.closest('tr').data('id')
   				console.log(id)
   				Load()
   				$.ajax({
   					type:'post',
   					data:{
   						car_audit_id:id
   					},
   					url:url.delete,
   					success:function(res){
   						Load(1)
   						if(res.code==1){
	   						Msg('删除成功');
	   						ajaxbuycar(data)
   						}else{
   							Err(res.msg)
   						}
   					},
   					error:function(){
   						Load(1)
   						Err('请求失败')
   					}
   				})
   				layer.close(auto)
   			})
   		}
   		})
	}
	//申请审核
	function propcheck(obj){
		
		var auto = layer.open({
   		type: 1,
   		title:'申请审核',
   		skin: 'layui-layer-rim', //加上边框
   		area: ['300px', '400px'], //宽高
   		content: `
   			<div class='am-u-sm-12' style='font-size:12px;padding:10px'>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					购车单位: ${obj.company_name}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					品牌:${obj.brand}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					承载人数:${obj.carry}
   				</div>
   				
   				<div class='am-u-sm-12 po_relative' style='margin-bottom:10px'>
   					<div class='am-fl buy_money'>购车预算:<span>${obj.budget}</span></div>
   					<div class='po_absolute' style='top:0;left:120px'>
   						<button class='am-btn am-btn-default am-btn-xs cg_money' >修改预算金额</button>
   						<div class='am-u-sm-12 cg_input_w' style='display:none ;z-index:9999'>
	   						<input type='number' class='cg_input' placeholder='请输入修改金额' style='width:100px' value='${obj.budget}'/>
	   						<button class='am-btn am-btn-default am-btn-xs cg_input_ok' >确定</button>
	   						<button class='am-btn am-btn-default am-btn-xs cg_input_no' >取消</button>
   						</div>
   					</div>
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					购车用途:${obj.purpose}
   				</div>
   				
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					购车理由:${obj.remark}
   				</div>
   				<div class='am-u-sm-12' style='text-align:center'>
   					<a class='am-btn am-btn-sm am-btn-primary am-radius apply_ok'>同意</a>
   					<a class='am-btn am-btn-sm am-btn-primary am-radius apply_no'>拒绝</a>
   				</div>
   			</div>
   		`,
   		success:function(){
   			//同意审核
   			$('.apply_ok').on('click',function(){
   				var budget=obj.budget
   				
   				console.log(obj.car_audit_id)
   				$.ajax({
   					type:'post',
   					url:url.check,
   					data:{
   						car_audit_id:obj.car_audit_id,
   						money:$('.buy_money span').html(),
   						state:2
   					},
   					success:function(res){
   						if(res.code==1){
   							Msg('审核通过')
   							ajaxbuycar(data)
   						}else{
   							Err(res.msg)
   							
   						}
   						layer.close(auto)
   					},
   					error:function(){
   						Err('请求失败')
   						layer.close(auto)
   					}
   				})
   			
   			})
   			//审核不通过
   			$('.apply_no').on('click',function(){
   				$.ajax({
   					type:'post',
   					url:url.check,
   					data:{
   						
   					},
   					success:function(res){
   						if(res.code==1){
   							Msg('审核通过')
   							ajaxbuycar(data)
   						}else{
   							Err(res.msg)
   							
   						}
   						layer.close(auto)
   					},
   					error:function(){
   						Err('请求失败')
   						layer.close(auto)
   					}
   				})
   			})
   			//修改预算金额
   			$('.cg_money').on('click',function(){
   				$(this).css('display','none')
   				$('.cg_input_w').css('display','block')
   			})
   			$('.cg_input_ok').on('click',function(){
   				$('.buy_money').html('购车预算:<span>'+$('.cg_input').val()+'</sapn>')
   				$('.cg_money').css('display','block')
   				$(this).parent().css('display','none')
   			})
   			$('.cg_input_no').on('click',function(){
   				$('.cg_money').css('display','block')
   				$(this).parent().css('display','none')
   			})
   		}
   		})
	}
	//查看详情
	function auditdetail(obj){
		var detailmain='';
		var lay_height=300;
		detailmain=`<div class='am-u-sm-12' style='font-size:12px;padding:10px'>
   				
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					购车单位: ${obj.company_name}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					品牌: ${obj.brand}
   				</div>`
		if(obj.state==2){
			lay_height="400px"
			detailmain+=`<div class='am-u-sm-12' style='margin-bottom:10px'>
   					申请时间: ${obj.date}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					审核时间: ${obj.date_2}
   				</div>`
		}else if(obj.state==3){
			lay_height="450px"
			detailmain+=`
				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					车牌号:<a href='{:url("detail")}'> ${obj.number}</a>
   				</div>
				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					申请时间: ${obj.date}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					审核时间: ${obj.date_2}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					登记时间: ${obj.date_4}
   				</div>	
			`
		}else if(obj.state>=4){
			lay_height="460px"
			detailmain+=`
				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					车牌号: <a href='{:url("detail")}'> ${obj.number?obj.number:'-'}</a>
   				</div>
				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					申请时间: ${obj.date?obj.date:'-'}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					审核时间: ${obj.date_2?obj.date_2:'-'}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					登记时间: ${obj.date_4?obj.date_4:'-'}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					车辆审批时间: ${obj.date_5?obj.date_5:'-'}
   				</div>	
			`
		}
		detailmain+=`<div class='am-u-sm-12' style='margin-bottom:10px'>
   					承载人数: ${obj.carry?obj.carry:'-'}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					申请人: ${obj.proposer?obj.proposer:'-'}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					购车预算: ${obj.budget?obj.budget:'-'}
   				</div>
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					购车用途: ${obj.purpose?obj.purpose:'-'}
   				</div>
   				
   				<div class='am-u-sm-12' style='margin-bottom:10px'>
   					购车理由: ${obj.remark?obj.remark:'-'}
   				</div>
   				<div class='am-u-sm-12' style='text-align:center'>
   					<a class='am-btn am-btn-sm am-btn-primary am-radius detail_close'>取消</a>
   				</div>
   				</div>`
		
		var detail = layer.open({
	   		type: 1,
	   		title:'审核详情',
	   		skin: 'layui-layer-rim', //加上边框
	   		area: ['300px',lay_height], //宽高
	   		content: detailmain,
	   		success:function(){
	   			$('.detail_close').on('click',function(){
	   				layer.close(detail)
	   			})
	   		}
	   		})
	}
})
